/*
 * 对Date.prototype添加原型链
 */
Date.prototype.format = function(_join){
	var dStr = _join.replace(/YYYY/g,this.getFullYear());
	dStr = dStr.replace(/YY/g,String(this.getYear()).slice(-2));
	dStr = dStr.replace(/MM/g,("0" + String(this.getMonth() + 1)).slice(-2));
	dStr = dStr.replace(/M/g,this.getMonth() + 1);
	
	dStr = dStr.replace(/DD/g,("0" + String(this.getDate())).slice(-2));
	dStr = dStr.replace(/D/g,this.getDate());
	
	dStr = dStr.replace(/hh/g,("0" + String(this.getHours())).slice(-2));
	dStr = dStr.replace(/h/g,this.getHours());
	
	dStr = dStr.replace(/mm/g,("0" + String(this.getMinutes())).slice(-2));
	dStr = dStr.replace(/m/g,this.getMinutes());
	
	dStr = dStr.replace(/ss/g,("0" + String(this.getSeconds())).slice(-2));
	dStr = dStr.replace(/s/g,this.getSeconds());
	return dStr;
}

/**
 * 返回时间属性集合，可以和String原型padLeft配合组装日期格式
 * eg: var d = new Date();
 * 	   d.year + d.month.padLeft(2, "0") + d.date.padLeft(2, "0");	//->20080501
 */
Date.prototype.getAttributes = function() {
	return {
		year : this.getFullYear().toString(),
		month: (this.getMonth()+1).toString(),
		date:   this.getDate().toString(),
		hour:  this.getHours().toString(),
		minute: this.getMinutes().toString(),
		second: this.getSeconds().toString(),
		
		day: this.getDay()
	}
}

/**
 * 返回与某日期的间隔
 * @param {Object} format	格式 day|month|year|second|minute|week
 * @param {Object} now	要比较的日期，可不传
 */
Date.prototype.dateDiff = function(format, now) {
	var ret = "";
	
	if (!now) {
		now = new Date();
	}
	
	var dateSpan = now - new Date(this.toDateString()).getTime();
	var timeSpan = now - this;
	switch (format) {
		case "day":
			ret = (dateSpan/3600/1000/24);
			break;
		case "week":
			ret = (dateSpan/3600/1000/24/7);
			break;
		case "month":
			ret = (dateSpan/3600/1000/24/30);
			break;
		case "year":
			ret = (dateSpan/3600/1000/24/30/365);
			break;
		case "second":
			ret = timeSpan/1000;
			break;
		case "minute":
			ret = timeSpan/1000/60;
			break;
		case "hour":
			ret = timeSpan/1000/60/60;
			break;
		default:
			ret = timeSpan;
			break;
	}
	
	return ret;
//	return timeSpan.format(format);
}
